package com.base.doublepoint;

import java.util.HashMap;

public class ReverseString {
    /**
     * 反转字符串
     *
     * @param s
     */
    public void reverseString(char[] s) {
        int left = 0, right = s.length - 1;
        while (left < right) {
            char tmp = s[left];
            s[left] = s[right];
            s[right] = tmp;
            left++;
            right--;
        }

    }

    public String reverseWords(String s) {
        StringBuffer ret = new StringBuffer();
        int length = s.length();
        int i = 0;
        while (i < length) {
            int start = i;
            while (i < length && s.charAt(i) != ' ') {
                i++;
            }
            for (int p = start; p < i; p++) {
                ret.append(s.charAt(start + i - 1 - p));
            }
            while (i < length && s.charAt(i) == ' ') {
                i++;
                ret.append(' ');
            }
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        ReverseString reverseString = new ReverseString();
        char[] s = new char[]{'d', 'e', 'f', 'g'};
        String ss ="asd wer d'dad";
        reverseString.reverseString(s);
        reverseString.reverseWords(ss);
    }
}
